package com.example.demo.controllers;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class FileUploadController {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
    @Value("${spring.servlet.multipart.location}")
    private String path;
    @PostMapping("/upload")
    public String upload(MultipartFile uploadFile)
    {
        Date date = new Date();
        String filePath = simpleDateFormat.format(date);
        String name = uploadFile.getOriginalFilename();
        File folder = new File(path,filePath);
        if(!folder.isDirectory()) folder.mkdirs();
        System.out.println(folder.getAbsolutePath());
        try {
            uploadFile.transferTo(new File(filePath,name));
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败";
    }
}
